package cn.mymall.web.controller;

import cn.mymall.constant.StatusConstant;
import cn.mymall.entity.mall.*;
import cn.mymall.service.mall.*;
import cn.mymall.vo.product.ProductSearchVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IAdvertService advertService;
    @Autowired
    private IPictureService pictureService;
    @Autowired
    private ISkuService skuService;
    @RequestMapping("/index")
    public String index(ModelMap model) {
        List<Category> categoryList = categoryService.list(new QueryWrapper<Category>().orderByAsc("sort_num")).subList(0, 8);
        List<Advert> advertList = advertService.list(new QueryWrapper<Advert>().eq("status", StatusConstant.Y).orderByAsc("sort_num")).subList(0, 3);
        ProductSearchVo search = new ProductSearchVo();
        search.setAsc(true);
        search.setSortField("sort_no");
        search.setPageNo(1);
        search.setPageSize(10);
        search.setStatus(StatusConstant.Y);
        Page<Product> page = productService.getProductList(search);
        model.put("advertList", advertList);
        model.put("categoryList", categoryList);
        model.put("productList", page.getRecords());
        return "front/index.ftl";
    }
    @RequestMapping("/product/{id}")
    public String product(@PathVariable int id, ModelMap model) {
        Product product = productService.getById(id);
        List<Picture> pictureList = pictureService.list(new QueryWrapper<Picture>().eq("product_id", id));
        List<Sku> skuList = skuService.findBookingByProductId(id);
        model.put("product", product);
        model.put("skuList", skuList);
        model.put("pictureList", pictureList);
        return "front/product.ftl";
    }

    @RequestMapping("/product/list/category/{id}")
    public String productList(@PathVariable int id, ProductSearchVo search, ModelMap model) {
        Category category = categoryService.getById(id);
        search.setCategoryId(id);
        List<Product> productList = productService.getProductList(search).getRecords();
        model.put("category", category);
        model.put("productList", productList);
        return "front/category_product_list.ftl";
    }

    @RequestMapping("/category/list")
    public String categoryList(ModelMap model) {
        List<Category> categoryList = categoryService.list(new QueryWrapper<Category>().gt("sort_num", 0).orderByAsc("sort_num"));
        model.put("categoryList", categoryList);
        return "front/category_list.ftl";
    }
    @RequestMapping("/searcher")
    public String searcher() {
        return "front/searcher.ftl";
    }
}
